<%@page import="com.elookinto.wordlist.MobileDeviceDetector, java.sql.*, javax.sql.*, com.elookinto.wordlist.Util"%>

<%@page contentType="text/html" pageEncoding="UTF-8" import="java.io.*, java.sql.*, javax.sql.*, org.brickred.socialauth.*"%>
<%

    boolean isMobile = isMobile(request, session);
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("Expires", -1);

    Cookie cookies[] = request.getCookies();
    Cookie myCookie = null;
    String cookieUid = "uid";

    if (cookies != null) {
        for (int i = 0; i < cookies.length; i++) {
            // out.println(cookies[i].getName() + "--- " + cookies[i].getValue() + "<br/>");
            if (cookies[i].getName().equals(cookieUid)) {
                myCookie = cookies[i];
                break;
            }

        }
    }
    String uid = null;
    String loginAccount = null;
    if (session.getAttribute("uid") == null) {
        SocialAuthManager manager = (SocialAuthManager) session.getAttribute("authManager");
        try {
            if (manager != null) {
                java.util.Map<String, String> paramsMap = org.brickred.socialauth.util.SocialAuthUtil.getRequestParametersMap(request);
                AuthProvider provider = manager.connect(paramsMap);
                if (provider != null) {
                    Profile p = provider.getUserProfile();
                    if (p != null && p.getEmail() != null) {
                        uid = p.getEmail();
                    } else {
                        uid = p.getFullName() + p.getDisplayName();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        loginAccount = uid;
    }

    if (uid != null) {
        session.setAttribute("uid", uid);
    } else {
        if (myCookie != null) {
            uid = myCookie.getValue();
            session.setAttribute("uid", uid);
        } else {
            uid = request.getRemoteHost();
            session.setAttribute("uid", uid);
        }
    }
    //out.println("uid=" + uid);
    Cookie cookie = new Cookie("uid", session.getAttribute("uid").toString());
    cookie.setMaxAge(365 * 24 * 60 * 60);
    response.addCookie(cookie);
    String text = "Not found";
    int end = 1;
    try {
        end = Integer.parseInt(request.getParameter("q"));
    } catch (Exception e) {
        e.printStackTrace();
    }
    int start = end -1;
    Connection con = null;
    try {
        con = Util.getCon();

        PreparedStatement st = con.prepareStatement("select limit ?,? d, subject, text from testingmom order by d desc ");
        st.setInt(1, start);
        st.setInt(2, end);
        ResultSet rs = st.executeQuery();
        if (rs.next()) {
            text = rs.getString("text");
        }
    } catch (Exception e) {
        if (con != null) {
            con.close();
        }

    }

%>
<!DOCTYPE html>
<html>
    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
        <meta name="title" content="Word List" />
        <meta name="description" content=" This word list app is the best tool to help boost your/your family vocabulary. " />
        <meta property="og:title" content="Word List" /> 
        <% if (isMobile) {%>
        <meta name="HandheldFriendly" content="true" />
        <meta name="viewport" content="width=device-width, height=device-height, user-scalable=no" />
        <%}%>
        <meta property="og:description" content="This word list app is the best tool to help boost your/your family vocabulary. " /> 
        <title> Word List </title>


        <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.0/themes/base/jquery-ui.css" />
        <link rel="stylesheet" type="text/css" href="css/wordlist.css"/> 
        <link rel="stylesheet" type="text/css" href="css/button.css"/> 
        <link rel="stylesheet" type="text/css" media="screen" href="css/atd.css" />
        <link rel="stylesheet" type="text/css" media="screen" href="js/qtip/jquery.qtip.min.css" />
        <link rel="stylesheet" type="text/css" href="js/jquery.contextMenu.css"/> 
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script src="http://code.jquery.com/ui/1.9.0/jquery-ui.js"></script>
        <script type="text/javascript" src="js/jquery.cookie.js"></script>
        <script type="text/javascript" src="js/jquery.limit.js"></script>
        <script src="js/jquery.atd.textarea.js"></script>
        <script src="js/qtip/jquery.qtip.min.js"> </script>
        <script src="js/jquery.contextMenu.js"> </script>
        <script src="js/csshttprequest.js"></script>

        <script type="text/javascript" src="js/wordlist.js"></script>

        <script type="text/javascript">

            var _gaq = _gaq || [];
            _gaq.push(['_setAccount', 'UA-9033082-1']);
            _gaq.push(['_trackPageview']);

            (function() {
                var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
                ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
                var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
            })();

        </script>
        <script type="text/javascript">
            $(document).ready(function() {
               
            });
        </script>



    </head>
    <body>
        <div class="wrapper">

            <div class="topmenu">  
                <%@include file="WEB-INF/jspf/menu.jspf" %>
            </div>
            <h2 align="center"> Questions
            </h2>
            <% if (!isMobile) {%>
            <div style="float:right" class="fb-like" data-href="http://www.elookinto.com/wordlist" data-send="true" data-width="450" data-show-faces="true"></div>
            <%}%>

            <!--
          <div><object id="test" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" 
                       codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" width="15" height="15"><PARAM NAME=movie VALUE="http://www.strangecube.com/audioplay/online/audioplay.swf?file=http://onelook.com/pronounce/macmillan/US/word-American-English-pronunciation.mp3&auto=no&sendstop=yes&repeat=1&buttondir=http://www.strangecube.com/audioplay/online/alpha_buttons/negative_small&bgcolor=0xffffff&mode=playpause"><PARAM NAME="allowScriptAccess" value="always" /> <PARAM NAME=quality VALUE=high><PARAM NAME=wmode VALUE=transparent><PARAM NAME=einterface VALUE=yes><embed einterface="yes" allowScriptAccess="always" name="test" 
                                                                                                                                                                                                    src="http://www.strangecube.com/audioplay/online/audioplay.swf?file=http://onelook.com/pronounce/macmillan/US/word-American-English-pronunciation.mp3&auto=no&sendstop=yes&repeat=1&buttondir=http://www.strangecube.com/audioplay/online/alpha_buttons/negative_small&bgcolor=0xffffff&mode=playpause&einterface=yes"
                                                                                                                                                                                                    quality=high wmode=transparent width="15" height="15" align="" TYPE="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object></div>
    
            <button onclick="play1('connection')"> test </button>
            -->


            <div style="text-align: center">


                <script type="text/javascript"><!--
                    google_ad_client = "ca-pub-1830488441040930";
                    /* wordlist */
                    google_ad_slot = "4413544198";
                    google_ad_width = 728;
                    google_ad_height = 90;
                    //-->
                </script>
                <script type="text/javascript"
                        src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
                </script><br/>

            </div>


        <h3 align="center"> <a href="?q=<%= end+1 %>"> Prev Question </a> &nbsp; &nbsp; <a href="?q=<%= end -1 %>"> Next Question </a>  </h3>
            <div id="wordlist" style="margin:30px" >
                <%= text%>
            </div>





            <div class="push"> </div>
            <div class='footer' style="width:100%;text-align:center">

                <%@include file="footer.jsp" %>
            </div>

        </div>


        <%-- nodisplayable--%>

        <div id="dialog" title="Choose a dictionary " style="background:yellowgreen;display: none">
            <p style="margin-top:15px"><span class="ui-icon ui-icon-message" style="float:left; margin:0 7px 20px 0;">
                </span>
            <table> <tr>
                    <td> One Look: </td> <td> <input type="checkbox" id="choose_0" /> </td></tr>
                <tr>
                    <td>Beginner's Wordsymth: </td> <td><input type="checkbox" id="choose_1" /> </td></tr>
                <tr>
                    <td> Children's Wordsymth:</td> <td> <input type="checkbox" id="choose_2" /> </td></tr>
                <tr>
                    <td>Advanced Wordsymth: </td> <td><input type="checkbox" id="choose_3" checked=true /></td></tr>
                <!-- <tr>
                    <td> Google Dict:</td> <td> <input type="checkbox" id="choose_4"  checked="true"/>
                    </td></tr> -->
            </table>

        </div>

        <ul id="renameList" class="contextMenu">
            <li class="edit">
                <a href="#rename">Rename List</a>
            </li>

        </ul>
        <div id="dialog-form" title="Rename List" style="width:450px; display:none;background:lightgoldenrodyellow">
            <form>
                <fieldset>
                    <label for="oldNameName"><b>Old List Name:</b></label>
                    <span id="oldListName"> </span> <br/> <br/>
                    <label for="newListName"><b>New list Name:</b></label> 
                    <input type="text" name="newListName" id="newListName" value="" class="text ui-widget-content ui-corner-all" />

                </fieldset>
            </form>
        </div>
        <%-- nodisplayable--%>

    </body>
</html>

<%!
    static boolean isMobile(HttpServletRequest request, HttpSession session) {
        if (session.getAttribute("mobile") != null) {
            return (Boolean) session.getAttribute("mobile");
        }
        String ua = request.getHeader("User-Agent").toLowerCase();
        java.util.regex.Pattern p = java.util.regex.Pattern.compile(".*(iPhone|mobile|iPod|blackberry|DoCoMo|cldc|android|htc|lg|midp|mmp|mobile|nokia|opera mini|palm|pocket|psp|sgh|smartphone|symbian|treo mini|Playstation Portable|SonyEricsson|Samsung|MobileExplorer|PalmSource|Benq|Windows Phone|Windows Mobile|IEMobile|Windows CE|Nintendo Wii).*", java.util.regex.Pattern.CASE_INSENSITIVE);
        java.util.regex.Matcher matcher = p.matcher(ua);
        if (matcher.matches()) {
            session.setAttribute("mobile", true);
            return true;
        } else {
            session.setAttribute("mobile", false);
            return false;
        }
    }

    enum Browser {

        IE,
        Firefox,
        CHROME;
    };

    static Browser browser(HttpServletRequest request) {
        String ua = request.getHeader("User-Agent");

        if (ua != null && ua.indexOf("MSIE") != -1) {
            return Browser.IE;
        }

        if (ua != null && ua.indexOf("Chrome/") != -1) {
            return Browser.CHROME;
        }
        //boolean isSafari = (!isChrome && (ua != null && ua.indexOf("Safari/") != -1));

        if (ua != null && ua.indexOf("Firefox/") != -1) {
            return Browser.Firefox;
        }
        return null;

    }
%>
